package com.kingdee.eas.custom.hr.applyinform.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.configure.ChangeType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hostelmgr.basedata.NeedRoomEnum;
import com.kingdee.eas.custom.hr.applyinform.AddrChangeApplyEntryFactory;
import com.kingdee.eas.custom.hr.applyinform.AddrChangeApplyEntryInfo;
import com.kingdee.eas.custom.hr.applyinform.AddrChangeApplyInfo;
import com.kingdee.eas.custom.hr.applyinform.AddrChangeEnum;
import com.kingdee.eas.custom.hr.applyinform.DomicileEnum;
import com.kingdee.eas.hr.base.HRBillBaseEntryInfo;
import com.kingdee.eas.hr.base.IHRBillBaseEntry;
import com.kingdee.eas.hr.base.app.IHRBillBizBean;
import com.kingdee.eas.util.app.DbUtil;

public class AddrChangeApplyControllerBean extends
		AbstractAddrChangeApplyControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hr.applyinform.app.AddrChangeApplyControllerBean");

	@Override
	protected String getBillField() throws BOSException, EASBizException {
		return "parent";
	}

	@Override
	protected IHRBillBizBean getBizBean() throws BOSException, EASBizException {
		return new AddrChangeApplyBizBean();
	}

	@Override
	protected String getEntryEffectDateField() throws BOSException,
			EASBizException {
		return "effectDate";
	}

	@Override
	protected String getEntryField() throws BOSException, EASBizException {
		return "entrys";
	}

	@Override
	protected IHRBillBaseEntry getEntryInterface(Context ctx)
			throws BOSException, EASBizException {
		return AddrChangeApplyEntryFactory.getLocalInstance(ctx);
	}

	@Override
	protected void _entryEffect(Context ctx, HRBillBaseEntryInfo entry)
			throws BOSException, EASBizException {
		super._entryEffect(ctx, entry);

		AddrChangeApplyEntryInfo entryInfo = (AddrChangeApplyEntryInfo) entry;
		// AddrChangeApplyInfo info = entryInfo.getParent();

		this.updatePerson(ctx, entryInfo);
	}

	private void updatePerson(Context ctx, AddrChangeApplyEntryInfo entryInfo)
			throws BOSException {

		String sql = null;
		String bus = entryInfo.getBus() == null ? null : entryInfo.getBus()
				.getId().toString();

		if (entryInfo.getDomicileType().equals(DomicileEnum.HOSTEL)) {
			sql = "UPDATE T_BD_PERSON SET CFCummutingCodeID = ?,CFIsRoom=?,CFBusID=?,CFStation=? WHERE FID=?";
			DbUtil.execute(ctx, sql, new Object[] {
					entryInfo.getNewCommutingCode().getId().toString(),
					NeedRoomEnum.YES_VALUE, bus, entryInfo.getBusStation(),
					entryInfo.getPerson().getId().toString() });
		} else {
			sql = "UPDATE T_BD_PERSON SET CFCummutingCodeID = ?,CFIsRoom=?,CFBusID=?,CFStation=? WHERE FID=?";

			DbUtil.execute(ctx, sql, new Object[] {
					entryInfo.getNewCommutingCode().getId().toString(),
					NeedRoomEnum.NO_VALUE, bus, entryInfo.getBusStation(),
					entryInfo.getPerson().getId().toString() });
		}

		sql = "UPDATE T_HR_PersonContactMethod SET FAddress = ? WHERE FPersonID=?";
		DbUtil.execute(ctx, sql, new Object[] { entryInfo.getNewAddress(),
				entryInfo.getPerson().getId().toString() });
	}

	@Override
	protected String getEffectFunction() throws BOSException, EASBizException {
		return "com.kingdee.eas.custom.hr.applyinform.app.AddrChangeApplyEffectFunction";
	}
}